package util

import (
	"crypto/sha1"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"gpt-api/framework/log"
	"net/http"
	"strconv"
	"time"
)

func Oppo(PageId int, Tid string, Lbid string) {
	token := authenticatedHTTPRequest()
	//log.Info("Oppo _ token:", log.Var("token", token))
	var result struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
	}

	headers := http.Header{}

	headers.Set("Content-Type", "application/json")
	headers.Set("Authorization", fmt.Sprintf("%s %s", "Bearer", token))
	url := "https://sapi.ads.oppomobile.com/v1/clue/sendData"
	params := map[string]interface{}{}
	params["pageId"] = PageId
	params["ownerId"] = 1000197816
	params["ip"] = "123.118.108.37"
	params["transformType"] = 8
	//params["tid"] = "1-8pMJ0rT32MGFH9vRgVscZtoDZttVmYGjt7ox2JebN8k5%2FGN8kglK%2BmE9AQgEaFPV1669370966ApiTestTidProfix"
	//params["lbid"] = 1_0_0
	params["tid"] = Tid
	params["lbid"] = Lbid

	//log.Info("OPPO_params", log.Var("params", params))
	ms, _ := json.Marshal(params)
	msg, code, respHeader, err := HttpReq(url, "POST", headers, nil, ms, &result)
	if err != nil {
		log.Error("oppo_httpReq", log.Var("err", err))
	}
	log.Info("OPPO归因上传", log.Var("msg", msg), log.Var("code", code), log.Var("respHeader", respHeader))
}

// 验签认证
func authenticatedHTTPRequest() string {

	timestamp := strconv.FormatInt(time.Now().Unix(), 10)

	var owner_id, api_id, api_key string
	owner_id = "1000197816"
	api_id = "20a09cb2459a4ba79cd89bc574a602fa"
	api_key = "f5e144b599e84edc801458aac57dcd92"

	p := fmt.Sprintf("%s%s%s", api_id, api_key, timestamp)

	arr := sha1.Sum([]byte(p))

	toString := hex.EncodeToString(arr[:])

	ps := fmt.Sprintf("%s,%s,%s,%s", owner_id, api_id, timestamp, toString)
	token := base64.StdEncoding.EncodeToString([]byte(ps))

	return token
}
func Oppo2(PageId int, Tid string, Lbid string) {
	token := authenticatedHTTPRequest2()
	//账号3
	var result struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
	}

	headers := http.Header{}

	headers.Set("Content-Type", "application/json")
	headers.Set("Authorization", fmt.Sprintf("%s %s", "Bearer", token))
	url := "https://sapi.ads.oppomobile.com/v1/clue/sendData"
	params := map[string]interface{}{}
	params["pageId"] = PageId
	params["ownerId"] = 1000197816
	params["ip"] = "123.118.108.37"
	params["transformType"] = 8
	//params["tid"] = "1-8pMJ0rT32MGFH9vRgVscZtoDZttVmYGjt7ox2JebN8k5%2FGN8kglK%2BmE9AQgEaFPV1669370966ApiTestTidProfix"
	//params["lbid"] = 1_0_0
	params["tid"] = Tid
	params["lbid"] = Lbid
	ms, _ := json.Marshal(params)
	msg, code, respHeader, err := HttpReq(url, "POST", headers, nil, ms, &result)
	if err != nil {
		log.Error("oppo_httpReq", log.Var("err", err))
	}
	log.Info("Oppo归因上传", log.Var("msg", msg), log.Var("code", code), log.Var("respHeader", respHeader))

}
func authenticatedHTTPRequest2() string {

	timestamp := strconv.FormatInt(time.Now().Unix(), 10)

	var owner_id, api_id, api_key string
	owner_id = "1000197816"
	api_id = "20a09cb2459a4ba79cd89bc574a602fa"
	api_key = "f5e144b599e84edc801458aac57dcd92"

	p := fmt.Sprintf("%s%s%s", api_id, api_key, timestamp)

	arr := sha1.Sum([]byte(p))

	toString := hex.EncodeToString(arr[:])

	ps := fmt.Sprintf("%s,%s,%s,%s", owner_id, api_id, timestamp, toString)
	token := base64.StdEncoding.EncodeToString([]byte(ps))

	return token
}
func Oppo3(PageId int, Tid string, Lbid string) {
	token := authenticatedHTTPRequest3()
	//log.Info("Oppo _ token:", log.Var("token", token))
	//账号3
	var result struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
	}

	headers := http.Header{}

	headers.Set("Content-Type", "application/json")
	headers.Set("Authorization", fmt.Sprintf("%s %s", "Bearer", token))
	url := "https://sapi.ads.oppomobile.com/v1/clue/sendData"
	params := map[string]interface{}{}
	params["pageId"] = PageId
	params["ownerId"] = 1000197815
	params["ip"] = "123.118.108.37"
	params["transformType"] = 8
	//params["tid"] = "1-8pMJ0rT32MGFH9vRgVscZtoDZttVmYGjt7ox2JebN8k5%2FGN8kglK%2BmE9AQgEaFPV1669370966ApiTestTidProfix"
	//params["lbid"] = 1_0_0
	params["tid"] = Tid
	params["lbid"] = Lbid

	//log.Info("OPPO_params", log.Var("params", params))
	ms, _ := json.Marshal(params)
	msg, code, respHeader, err := HttpReq(url, "POST", headers, nil, ms, &result)
	if err != nil {
		log.Error("oppo_httpReq", log.Var("err", err))
	}
	log.Info("OPPO归因上传", log.Var("msg", msg), log.Var("code", code), log.Var("respHeader", respHeader))

}
func authenticatedHTTPRequest3() string {

	timestamp := strconv.FormatInt(time.Now().Unix(), 10)

	var owner_id, api_id, api_key string
	owner_id = "1000197815"
	api_id = "029e3303eddf440fa0dfe88631e79ad9"
	api_key = "c0b8532b69f54ef18f93caa7b0ae473d"

	p := fmt.Sprintf("%s%s%s", api_id, api_key, timestamp)

	arr := sha1.Sum([]byte(p))

	toString := hex.EncodeToString(arr[:])

	ps := fmt.Sprintf("%s,%s,%s,%s", owner_id, api_id, timestamp, toString)
	token := base64.StdEncoding.EncodeToString([]byte(ps))

	return token
}
